package com.xintonghua.bussiness.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.CubeBean;
import com.xintonghua.bussiness.ui.fragment.cube.turnover.TurnoverActivity;
import com.xintonghua.bussiness.util.JumpUtils;

import org.w3c.dom.Text;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/1/12.
 * 包名：com.xintonghua.bussiness.adapter
 */

public class StockAdapter extends BaseAdapter<CubeBean, StockAdapter.StockViewHolder> {

    int type = 0;//0服务   1产品  (因为0和1界面都一样所以就传1)

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public StockAdapter(Activity mActivity, List<CubeBean> list) {
        super(mActivity, list);
    }

    @Override
    public StockViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_stock, parent, false);
        return new StockViewHolder(view);
    }

    AdapterView.OnItemClickListener itemClickListener;

    @Override
    public void onBindViewHolder(StockViewHolder holder, final int position) {
        CubeBean cubeBean = list.get(position);
        holder.ivBelate.setImageResource(cubeBean.getIcon());
        holder.tvProductName.setText(cubeBean.getName());
        if (type == 2) {
            holder.tvProductMoney.setText("-" + cubeBean.getInCome() + "元");
        } else {
            holder.tvProductMoney.setText((cubeBean.isStatistics() ? "+" : "+") + cubeBean.getInCome() + "元");
        }

        holder.ivStatistics.setVisibility(cubeBean.isStatistics() ? View.VISIBLE : View.GONE);
        switch (type) {
            case 0:
                holder.tvVaseNumber.setText("x" + cubeBean.getNumber() + "");
                break;
            case 1:
                holder.tvVaseNumber.setText("x" + cubeBean.getNumber() + "件");
                break;
            case 2:
                holder.tvVaseNumber.setText("x" + cubeBean.getNumber() + "件");
                break;
        }

        holder.tvVaseNumber.setVisibility(cubeBean.isStatistics() ? View.GONE : View.VISIBLE);
        if (!TextUtils.isEmpty(cubeBean.getInCome())) {
            if (type != 2)
                holder.tvProductMoney.setTextColor(ContextCompat.getColor(mActivity, Double.parseDouble(cubeBean.getInCome()) >= 0 ? R.color.red : R.color.green));
        } else {
            holder.tvProductMoney.setTextColor(ContextCompat.getColor(mActivity, R.color.red));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(null, view, position, 0);
                }
//                JumpUtils.jumpto(mActivity, TurnoverActivity.class, null);
            }
        });
    }

    public void setOnItemsClistener(AdapterView.OnItemClickListener itemClistener) {
        this.itemClickListener = itemClistener;
    }


    class StockViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_belate)
        ImageView ivBelate;
        @BindView(R.id.tv_product_name)
        TextView tvProductName;
        @BindView(R.id.tv_product_money)
        TextView tvProductMoney;
        @BindView(R.id.tv_vase_number)
        TextView tvVaseNumber;
        @BindView(R.id.iv_statistics)
        ImageView ivStatistics;

        public StockViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
